package com.fxd.shopping.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.fxd.shopping.product.dto.AttrGroupWithAttrDTO;
import com.fxd.model.product.AttrEntity;
import com.fxd.shopping.product.param.AttrGroupRelationDelAndAddParam;
import com.fxd.shopping.product.service.AttrAttrgroupRelationService;
import com.fxd.shopping.product.service.AttrService;
import com.fxd.shopping.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.fxd.model.product.AttrGroupEntity;
import com.fxd.shopping.product.service.AttrGroupService;
import com.fxd.core.util.PageUtils;
import com.fxd.core.util.RR;


/**
 * 属性分组
 *
 * @author fuxdong
 * @email 2016335176@qqcom
 * @date 2021-07-17 10:30:51
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public RR list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") long catelogId) {

        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return RR.ok().put("page", page);
    }

    /**
     * 属性分组关联关系列表
     *
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    private RR attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {

        List<AttrEntity> list = attrService.getRelationAttr(attrgroupId);
        return RR.ok().put("data", list);
    }

    /**
     * 分组新增关联关系列表
     *
     * @param attrgroupId
     * @param params
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    private RR attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId, @RequestParam Map<String, Object> params) {

        PageUtils page = attrService.getNoRelationAttr(params, attrgroupId);
        return RR.ok().put("data", page);
    }

    /**
     * 获取某一个分类下所有属性分组以及属性
     *
     * @param catelogId
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public RR getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {

        //1，查出当前分类下的所有属性分组
        //2，查出每个属性分组的所有属性
        List<AttrGroupWithAttrDTO> list = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return RR.ok().put("data", list);
    }

    @PostMapping("/attr/relation")
    public RR addRelation(@RequestBody List<AttrGroupRelationDelAndAddParam> params) {
        attrAttrgroupRelationService.saveBatch(params);
        return RR.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public RR info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);

        return RR.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public RR save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return RR.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public RR update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return RR.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public RR delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return RR.ok();
    }

    @PostMapping("/attr/relation/delete")
    public RR deleteRelation(@RequestBody List<AttrGroupRelationDelAndAddParam> param) {

        attrService.deleteRelation(param);
        return RR.ok();
    }

}
